#!/bin/bash

# postinst script for drv-zfs
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
        if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/lib/fs/zfs/bootinstall ] && rm -f $BASEDIR/usr/lib/fs/zfs/bootinstall
	else
	    chmod 0555 "$BASEDIR/usr/lib/fs/zfs/bootinstall"
	    chown root:bin "$BASEDIR/usr/lib/fs/zfs/bootinstall"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/kmdb/amd64/zfs ] && rm -f $BASEDIR/kernel/kmdb/amd64/zfs
	else
	    chmod 0555 "$BASEDIR/kernel/kmdb/amd64/zfs"
	    chown root:sys "$BASEDIR/kernel/kmdb/amd64/zfs"
	fi
	chmod 0555 "$BASEDIR/usr/lib/mdb/kvm/amd64/zfs.so"
	chown root:sys "$BASEDIR/usr/lib/mdb/kvm/amd64/zfs.so"
	chmod 0555 "$BASEDIR/usr/lib/mdb/proc/amd64/libzpool.so"
	chown root:sys "$BASEDIR/usr/lib/mdb/proc/amd64/libzpool.so"
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/sysevent/config/SUNW,EC_zfs,ESC_ZFS_bootfs_vdev_attach,sysevent.conf ] && rm -f $BASEDIR/etc/sysevent/config/SUNW,EC_zfs,ESC_ZFS_bootfs_vdev_attach,sysevent.conf
	else
	    chmod 0644 "$BASEDIR/etc/sysevent/config/SUNW,EC_zfs,ESC_ZFS_bootfs_vdev_attach,sysevent.conf"
	    chown root:sys "$BASEDIR/etc/sysevent/config/SUNW,EC_zfs,ESC_ZFS_bootfs_vdev_attach,sysevent.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/zfs.conf ] && rm -f $BASEDIR/kernel/drv/zfs.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/zfs.conf"
	    chown root:sys "$BASEDIR/kernel/drv/zfs.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/zfs ] && rm -f $BASEDIR/kernel/drv/amd64/zfs
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/zfs"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/zfs"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/lib/devfsadm/linkmod/SUNW_zfs_link.so ] && rm -f $BASEDIR/usr/lib/devfsadm/linkmod/SUNW_zfs_link.so
	else
	    chmod 0755 "$BASEDIR/usr/lib/devfsadm/linkmod/SUNW_zfs_link.so"
	    chown root:sys "$BASEDIR/usr/lib/devfsadm/linkmod/SUNW_zfs_link.so"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/lib/sysevent/modules/zfs_mod.so ] && rm -f $BASEDIR/usr/lib/sysevent/modules/zfs_mod.so
	else
	    chmod 0755 "$BASEDIR/usr/lib/sysevent/modules/zfs_mod.so"
	    chown root:sys "$BASEDIR/usr/lib/sysevent/modules/zfs_mod.so"
	fi
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/fs/amd64 && cd $BASEDIR/kernel/fs/amd64 && cp -f ../../../kernel/drv/amd64/zfs zfs)
	[ "$ZONEINST" = "1" ] || (grep -c "^zfs " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m '* 0600 root sys','zfs 0666 root sys' zfs ) )
	
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

